﻿/* Copyright (c) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Google.GData.Apps;

namespace MobileGData
{
    public partial class Form1 : Form
    {
        private AppsService service;

        public Form1()
        {
            InitializeComponent();
        }

        //Shows the panel passed and hides all others
        private void togglePanel(Control panel)
        {
            foreach ( Control control in this.Controls )
            {
                if (control is Panel)
                {
                    if (control.Equals(panel))
                    {
                        control.Show();
                    }
                    else
                    {
                        control.Hide();
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            togglePanel(panel1);
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            togglePanel(panel1);    
        }

        private void menuRetrieve_Click(object sender, EventArgs e)
        {
            togglePanel(panel2);  
            try
            {
                UserEntry entry = service.RetrieveUser(newUserName.Text.Trim());
             }
            catch (AppsException exception)
            {
                statusBar1.Text = exception.Reason;
            }
        }

        private void menuCreate_Click(object sender, EventArgs e)
        {
            togglePanel(panel2);
        }

        private void btnCreateUser_Click(object sender, EventArgs e)
        {
            try
            {
                UserEntry entry = service.CreateUser(newUserName.Text.Trim(), firstname.Text.Trim(), familyname.Text.Trim(), password.Text.Trim());
                statusBar1.Text = entry.Login.UserName + " created successfully";
            }
            catch (AppsException exception)
            {
                statusBar1.Text = exception.Reason;
            }
        }

        private void menuRestore_Click(object sender, EventArgs e)
        {
            togglePanel(panel3);
            retreiveAll();
        }


        private void retreiveAll()
        {
            UserFeed feed = service.RetrieveAllUsers();
      
            foreach (UserEntry entry in feed.Entries)
            {
                comboBox1.Items.Add(entry.Login.UserName);
            }
        }

        private void menuSuspend_Click(object sender, EventArgs e)
        {
            togglePanel(panel3); 
            retreiveAll();
        }

        private void menuItem10_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                String domainName = textBox2.Text.Split(new char[] { '@' })[1];
                service = new AppsService(domainName, textBox2.Text, textBox1.Text);
                UserEntry entry = service.RetrieveUser(textBox2.Text.Split(new char[] { '@' })[0]);
                panel1.Hide();
                statusBar1.Text = "Hello " + entry.Login.UserName;
                mItemLogin.Enabled = false;
                mnuCreate.Enabled = true;
                mnuRetrieve.Enabled = true;
                mnuRestore.Enabled = true;
                mnuSuspend.Enabled = true;
            }
            catch (AppsException exception)
            {
                statusBar1.Text = exception.Reason;
            }

        }
    }
}